package com.myteam.fugaocrm.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import com.myteam.fugaocrm.service.LoginServiceImpl;
import com.myteam.fugaocrm.service.RemindMessageServiceImpl;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
	@Resource protected LoginServiceImpl loginServiceImpl;
	@Resource protected RemindMessageServiceImpl remindMessageServiceImpl;
	
	@RequestMapping("login/login")
	@ResponseBody
	public ModelAndView login(HttpServletRequest request, Model m) throws Exception {
		String username = request.getParameter("username");  
		String password = request.getParameter("password");  
		
		Map<String, String> map = loginServiceImpl.login(username, password);
		
		ModelAndView view = new ModelAndView();
		
		if("1".equals(map.get("success").toString())) {
			//查询提醒数量(当前用户的提醒数量)
			Integer remindCount = remindMessageServiceImpl.selectRemindCount(Integer.parseInt(map.get("staffId").toString()));
			
			request.getSession().setAttribute("staffId", map.get("staffId"));
			request.getSession().setAttribute("staffName", map.get("staffName"));
			request.getSession().setAttribute("permission", map.get("permission"));
			request.getSession().setAttribute("remindCount", remindCount);
			
			view.setViewName("index.jsp");
		}else {
			view.addObject("msg", map.get("msg"));
			view.setViewName("login.jsp");
		}
		return view;
	}
	
	/**
	 * 查询提醒数量
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("login/remind")
	@ResponseBody
	public Integer remind(HttpServletRequest request) throws Exception {
		Integer staffId = Integer.parseInt(request.getParameter("staffId").toString());  
		
		//查询提醒数量(当前用户的提醒数量)
		Integer remindCount = remindMessageServiceImpl.selectRemindCount(staffId);
		
		return remindCount;
	}
}
